package com.geo.geotool.global;

import org.geotools.factory.CommonFactoryFinder;
import org.geotools.styling.Stroke;
import org.jaitools.numeric.Range;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.geotools.styling.*;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Function;

public class Constants {
    public static List<Range> tempRangeList = new ArrayList<>();


    static {
        // 最高温 40 最低温 -20,每4度一个区间, 参考中国气象局
        for(int i = -20 ; i < 40 ; i = i + 4){
            tempRangeList.add(Range.create(i,true, i+4,false));
        }
    }


//    public static Style tminPolygonStyle ;
    public static Style getTminStyle(){
        StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
        FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2();
        // 创建边框样式（Stroke）
        Stroke stroke = styleFactory.createStroke(
                filterFactory.literal(Color.BLACK), // 边框颜色
                filterFactory.literal(1), // 边框宽度
                filterFactory.literal(1)// 边框透明度
        );
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
        // rgb值
        int[][] rgbs = new int[10][3];
        rgbs[0] = new int[]{101,133,143};
        rgbs[1] = new int[]{59, 227, 233};
        rgbs[2] = new int[]{181, 251, 198};
        rgbs[3] = new int[]{207, 248, 226};
        rgbs[4] = new int[]{251, 249, 210};
        rgbs[5] = new int[]{231, 229, 175};
        rgbs[6] = new int[]{254, 226, 123};
        rgbs[7] = new int[]{224, 224, 28};
        rgbs[8] = new int[]{252, 166, 65};
        rgbs[9] = new int[]{250, 110, 5};
        // 这里偷懒，只做了2级至11级的样式，实际上应该根据实际情况做更多的样式
        for( int i = 2 ; i <= 11 ; i++){
            int[] rgb = rgbs[i-2];
            // 创建填充样式（Fill）
            Fill fill = styleFactory.createFill(
                    filterFactory.literal( new Color(rgb[0],rgb[1],rgb[2]))
            );
            Rule rule = styleFactory.createRule();
            Function parseDoubleFunction = filterFactory.function("parseDouble", filterFactory.property("value"));
            rule.setFilter(filterFactory.equals(parseDoubleFunction, filterFactory.literal(i)));
            Symbolizer symbolizer = styleFactory.createPolygonSymbolizer(stroke, fill, "the_geom");
            rule.symbolizers().add(symbolizer);
            fts.rules().add(rule);
        }
        Style style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);
        return style;
    }


    public static void main(String[] args) {
        System.out.println(Constants.tempRangeList.size());
        for( Range range : tempRangeList){
            System.out.println(range.getMin()+" - "+range.getMax());
        }
    }
}
